from flask import Flask, request

from admin.books_borrow import books_borrow_bp, books_borrow_view
from admin.books_borrow2 import books_borrow2_bp, books_borrow2_view

from extension import db, CORS
from admin.admin import admin_bp, admin_view
from admin.book import books_bp, books_view
from admin.user import user_bp, user_view
from admin.info import admin_info_bp, admin_info_view
from admin.user2 import user2_bp, user2_view
from admin.qa import qa_view,qa_bp
from student.history import students_history_bp, students_history_view
from user.info import user_info_bp, user_info_view
from user.login import user_login_bp, user_login_view
from user.books_rec import user_books_rec_bp, user_books_rec_view
from user.books import user_books_bp, user_books_view
from user.books2 import user_books2_bp, user_books2_view
from user.books3 import user_books3_bp, user_books3_view
from user.letters import user_letter_bp,user_letter_view
from user.overdue_pay import overdue_pay_bp, overdue_pay_view
from teacher.history import teachers_history_bp, teachers_history_view
from admin.returnMessage import returnMessage_bp,returnMessage_view
# 登录页面
# from forms import Login
from models import Admin
# from flask_login import UserMixin, LoginManager, login_required, login_user, logout_user, current_user

# 首页统计图
import time, datetime

# 使用设置MySQL连接
app = Flask(__name__)
cors = CORS(app, supports_credentials=True, resources=r"/*")
cors.init_app(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:123456@127.0.0.1/library_database'
db.init_app(app)

app.register_blueprint(admin_bp)
app.register_blueprint(books_bp)
app.register_blueprint(books_borrow_bp)
app.register_blueprint(books_borrow2_bp)
app.register_blueprint(user_bp)
app.register_blueprint(user2_bp)
app.register_blueprint(students_history_bp)
app.register_blueprint(teachers_history_bp)
app.register_blueprint(user_info_bp)
app.register_blueprint(admin_info_bp)

app.register_blueprint(qa_bp)

app.register_blueprint(user_login_bp)

app.register_blueprint(user_books_rec_bp)
app.register_blueprint(user_books_bp)
app.register_blueprint(user_books2_bp)
app.register_blueprint(user_books3_bp)
app.register_blueprint(user_letter_bp)
app.register_blueprint(overdue_pay_bp)

#短信路由
app.register_blueprint(returnMessage_bp)


# 注册admin的链接
app.add_url_rule('/admins/admins/', defaults={'admin_account': None}, view_func=admin_view, methods=['GET', ])
app.add_url_rule('/admins/admins/', view_func=admin_view, methods=['POST', ])
app.add_url_rule('/admins/admins/<admin_account>', view_func=admin_view, methods=['GET', 'PUT', 'DELETE'])

app.add_url_rule('/admins/books/', defaults={'book_id': None}, view_func=books_view, methods=['GET', ])
app.add_url_rule('/admins/books/', view_func=books_view, methods=['POST', ])
app.add_url_rule('/admins/books/<book_id>', view_func=books_view, methods=['GET', 'PUT', 'DELETE'])

app.add_url_rule('/admins/users/', defaults={'username': None}, view_func=user_view, methods=['GET', ])
app.add_url_rule('/admins/users/', view_func=user_view, methods=['POST'])
app.add_url_rule('/admins/users/<username>', view_func=user_view, methods=['GET', ])
app.add_url_rule('/admins/users/get_by_cardID/', defaults={'user_cardID': None}, view_func=user2_view, methods=['GET', ])
app.add_url_rule('/admins/users/get_by_cardID/<user_cardID>', view_func=user2_view, methods=['GET', ])
app.add_url_rule('/admins/users/<user_cardID>', view_func=user_view, methods=['PUT', 'DELETE'])

app.add_url_rule('/admins/books_borrow/', defaults={'book_id': None}, view_func=books_borrow_view, methods=['GET'])
app.add_url_rule('/admins/books_borrow/borrower_id/<borrower_id>', view_func=books_borrow2_view, methods=['GET'])
app.add_url_rule('/admins/books_borrow/<book_id>', view_func=books_borrow_view, methods=['GET', 'DELETE'])
app.add_url_rule('/admins/books_borrow/', view_func=books_borrow_view, methods=['POST'])
#上面这个post我改掉了
app.add_url_rule('/admins/info/', view_func=admin_info_view, methods=['GET', 'POST'])

#咨询api
app.add_url_rule('/admins/qaReply/', defaults={'qa_id': None}, view_func=qa_view, methods=['GET', ])
app.add_url_rule('/admins/qaReply/', view_func=qa_view, methods=['POST', ])
app.add_url_rule('/admins/qaReply/<qa_id>', view_func=qa_view, methods=['GET', 'PUT', 'DELETE'])

app.add_url_rule('/admins/books/', defaults={'book_id': None}, view_func=books_view, methods=['GET', ])
app.add_url_rule('/admins/books/', view_func=books_view, methods=['POST', ])
app.add_url_rule('/admins/books/<book_id>', view_func=books_view, methods=['GET', 'PUT', 'DELETE'])

app.add_url_rule('/students/history/<user_id>', view_func=students_history_view, methods=['GET', ])

app.add_url_rule('/teachers/history/<user_id>', view_func=teachers_history_view, methods=['GET', ])

app.add_url_rule('/users/books_rec/<cardID>', view_func=user_books_rec_view, methods=['GET'])
app.add_url_rule('/users/books/', defaults={'book_id': None}, view_func=user_books_view, methods=['GET'])
app.add_url_rule('/users/books/<book_id>', view_func=user_books_view, methods=['GET'])
app.add_url_rule('/users/books/by_book_id/', defaults={'book_id': None}, view_func=user_books2_view, methods=['GET'])
app.add_url_rule('/users/books/by_book_id/<book_id>', view_func=user_books2_view, methods=['GET'])
app.add_url_rule('/users/books/by_book_name/', defaults={'book_name': None}, view_func=user_books3_view, methods=['GET'])
app.add_url_rule('/users/books/by_book_name/<book_name>', view_func=user_books3_view, methods=['GET'])
app.add_url_rule('/users/login/<username>', view_func=user_login_view, methods=['GET'])
app.add_url_rule('/users/info', view_func=user_info_view, methods=['GET'])
app.add_url_rule('/users/letters', view_func=user_letter_view, methods=['GET', 'POST'])
app.add_url_rule('/users/overdue_pay/<cardID>/<money>', view_func=overdue_pay_view, methods=['PUT'])

#注册短信链接
app.add_url_rule('/admins/returnMessage/', view_func=returnMessage_view, methods=['GET',])


# 默认页面
@app.route('/')
def hello_world():  # put application's code here
    # 数据库连接测试
    # admin = get_admin_details('j2ee')
    # add_admin('test', 'test')
    return 'hello world'


# 统计图
# @app.route('/chart', methods=['GET'])
# @login_required
# def chart():
#     days = []
#     num = []
#     today_date = datetime.date.today()
#     today_str = today_date.strftime("%Y-%m-%d")
#     today_stamp = time.mktime(time.strptime(today_str + ' 00:00:00', '%Y-%m-%d %H:%M:%S'))
#     ten_ago = int(today_stamp) - 9 * 86400
#     for i in range(0, 10):
#         borr = ReadBook.query.filter_by(start_date=str((ten_ago + i * 86400) * 1000)).count()
#         retu = ReadBook.query.filter_by(end_date=str((ten_ago + i * 86400) * 1000)).count()
#         num.append(borr + retu)
#         days.append(timeStamp((ten_ago + i * 86400) * 1000))
#     # data = []
#     for i in range(0, 10):
#         item = {'name': days[i], 'num': num[i]}
#         data.append(item)
#     return jsonify(data)


if __name__ == '__main__':
    app.run()
